package com.clippick.walkthrough;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import co.kr.clippick.R;

public class ImagePageAdapter extends PagerAdapter {

	// common
	private Context mContext;
	private LayoutInflater inflater;

	// view
	private ImageView image;

	// data
	private List<Drawable> pages = new ArrayList<Drawable>();

	public ImagePageAdapter(Context context, List<Drawable> pageList) {
		// TODO Auto-generated constructor stub
		mContext = context;
		pages.removeAll(this.pages);
		pages.addAll(pageList);
		inflater = (LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return pages.size();
	}

	@Override
	public boolean isViewFromObject(View v, Object obj) {
		// TODO Auto-generated method stub
		return v == obj;
	}

	@Override
	public Object instantiateItem(View pager, int position) {
		View v = null;
		v = inflater.inflate(R.layout.walkthrough_image_viewer, null);

		image = (ImageView) v.findViewById(R.id.image);
		image.setImageDrawable(pages.get(position));

		image.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				((Activity) mContext).finish();
				((Activity) mContext).overridePendingTransition(
						R.anim.top_to_bottom, R.anim.top_to_bottom);
			}
		});

		((ViewPager) pager).addView(v, 0);

		return v;
	}

	@Override
	public void destroyItem(View pager, int position, Object view) {
		((ViewPager) pager).removeView((View) view);
	}

	@Override
	public void restoreState(Parcelable arg0, ClassLoader arg1) {

	}

	@Override
	public Parcelable saveState() {
		return null;
	}

	@Override
	public void startUpdate(View v) {
	}

	@Override
	public void finishUpdate(View v) {
	}
}
